package com.zihan.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zihan.constant.ParamsConstant;
import com.zihan.domain.dto.ProjectDTO;
import com.zihan.domain.entity.ProjectMember;
import com.zihan.domain.entity.ProjectMilestone;
import com.zihan.domain.result.PageResult;
import com.zihan.domain.result.Result;
import com.zihan.domain.vo.ProjectVO;
import com.zihan.service.IProjectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 科研项目表 前端控制器
 * </p>
 *
 * @author zhangzihan
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Resource
    private IProjectService projectService;

    /**
     * 创建项目
     */
    @PostMapping("/create")
    public Result<String> create(@RequestBody ProjectDTO project) {
        return projectService.create(project);
    }

    /**
     * 获取项目列表信息
     */
    @GetMapping("/teacher/list")
    public Result<PageResult> list(ProjectDTO  projectDTO) {
        return projectService.getList(projectDTO);
    }

    /**
     * 获取项目详情
     */
    @GetMapping("/detail/{id}")
    public Result<ProjectVO> detail(@PathVariable Long id) {
        return projectService.detail(id);
    }


    /**
     * 获取项目成员
     */
    @GetMapping("/member/{id}")
    public Result<List<ProjectMember>> member(@PathVariable Long id) {
        return projectService.member(id);
    }


    // 获取项目节点以及文档列表
    @GetMapping("/milestonDetail/{id}")
    public Result<List<ProjectMilestone>> getMilestonDetail(@PathVariable Long id) {
        return projectService.getMilestonDetail(id);
    }

    // 删除项目节点
    @DeleteMapping("/milestone/{id}")
    public Result<String> deleteMilestone(@PathVariable Long id) {
        return projectService.deleteMilestone(id);
    }


    // 更新项目信息
    @PutMapping("/update/project")
    public Result<String> updateProject(@RequestBody ProjectDTO project) {
        return projectService.updateProject(project);
    }

    //根据id删除项目
    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id) {
        if (Objects.isNull(id)) {
            return Result.error(ParamsConstant.PARAMS_ERROR);
        }
        return projectService.deleteByProjectId(id);
    }

    // 邀请学生
    @PostMapping("/invite")
    public Result<String> invite(@RequestBody JSONObject params) {
        return projectService.invite(params);
    }


    // 获取学生自己的邀请列表
    @GetMapping("/student/invitations")
    public Result<PageResult> invitationsStudentList(ProjectDTO projectDTO) {
        return projectService.invitationsStudentList(projectDTO);
    }


    // 接受项目邀请
    @PutMapping("/accept/invite/{id}")
    public Result<String> acceptInvitation(@PathVariable Long id) {
        return projectService.acceptInvitation(id);
    }

    // 拒绝项目邀请
    @PutMapping("/refuse/invite/{id}")
    public Result<String> refuseInvitation(@PathVariable Long id,String  reason) {
        return projectService.refuseInvitation(id,reason);
    }


    // 获取学生自己的项目节点
    @GetMapping("/student/milestone/{id}")
    public Result<List<ProjectMilestone>> getStudentMilestone(@PathVariable Long id) {
        if (Objects.isNull(id)){
            return Result.error(ParamsConstant.PARAMS_ERROR);
        }
        return projectService.getStudentMilestone(id);
    }


    // 学生上传节点信息
        @PostMapping("/student/upload/milestone")
    public Result<String> uploadMilestone(@RequestBody JSONObject params) {
        return projectService.uploadMilestone(params);
    }

    // 删除节点文档
    @DeleteMapping("/milestone/document/{id}")
    public Result<String> deleteMilestoneDocument(@PathVariable Long id) {

        if (Objects.isNull(id)) {
            return Result.error(ParamsConstant.PARAMS_ERROR);
        }
        return projectService.deleteMilestoneDocument(id);
    }


    // 移除项目成员
    @DeleteMapping("/remove/member/{id}")
    public Result<String> removeMember(@PathVariable Long id) {
        return projectService.removeMember(id);
    }


    // 获取学生自己的项目列表
    @GetMapping("/student/project/list")
    public Result<PageResult> getStudentProjectList(ProjectDTO projectDTO) {
        return projectService.getStudentProject(projectDTO);
    }
}
